
source('code/analysis/load.R')


data = read_dta('data/working/defund-sender-replication.dta')


main_result_tweet = function(data) {
  
  ## Panel A: Num of followers
  title = 'Experiment 1R: heterogeneity by Twitter characteristics'
  depvar = '\\textit{Scheduled Tweet}'
  label = 'sender-twitter-heterogeneity'
  
    
  model1 = lm(posted~excuse+age+I(age^2)+race+hisp+male+education, data=data %>% filter(num_follower_bin == 1))
  model2 = lm(posted~excuse+age+I(age^2)+race+hisp+male+education, data=data %>% filter(num_follower_bin == 2))
  model3 = lm(posted~excuse+age+I(age^2)+race+hisp+male+education, data=data %>% filter(num_follower_bin == 3))

  mean =   paste(unlist(formatC(c(mean(data$posted[data$excuse==0 & data$num_follower_bin == 1], na.rm = T),
                                  mean(data$posted[data$excuse==0 & data$num_follower_bin == 2], na.rm = T),
                                  mean(data$posted[data$excuse==0 & data$num_follower_bin == 3], na.rm = T)), digits=3)), collapse=' & ')
  
  out = stargazer(list(model1, model2, model3), 
                  omit='age|race|hisp|male|rep|education|Constant|partisan',
                  covariate.labels = c('Cover'),
                  keep.stat = c('rsq','adj.rsq', 'n'), dep.var.labels = depvar,
                  column.labels = c('0--25', '26--100', '100+'),
                  title = title, label = str_interp('t:${label}'),
                  add.lines = list(c('Controls', c('Yes', 'Yes', 'Yes'))))
  
  means1 = str_interp('No Cover mean & ${mean} \\\\')
  out1 = star_notes_tex(out, note.type = 'threeparttable', note = tablenotes[["sender-twitter-heterogeneity"]])
  
  ## Panel B: Percentage of audience who oppose
  title = 'Replication of Experiment 1 (by Twitter characteristics)'
  depvar = '\\textit{Scheduled Tweet}'
  label = 'main-tweet'
  
  
  model1 = lm(posted~excuse+age+I(age^2)+race+hisp+male+education, data=data %>% filter(perc_audi == 1))
  model2 = lm(posted~excuse+age+I(age^2)+race+hisp+male+education, data=data %>% filter(perc_audi == 2))
  model3 = lm(posted~excuse+age+I(age^2)+race+hisp+male+education, data=data %>% filter(perc_audi == 3))

  mean =   paste(unlist(formatC(c(mean(data$posted[data$excuse==0 & data$perc_audi == 1], na.rm = T),
                                  mean(data$posted[data$excuse==0 & data$perc_audi == 2], na.rm = T),
                                  mean(data$posted[data$excuse==0 & data$perc_audi == 3], na.rm = T)
  ), digits=3)), collapse=' & ')
  
  out = stargazer(list(model1, model2, model3), 
                  omit='age|race|hisp|male|rep|education|Constant|partisan',
                  covariate.labels = c('Cover'),
                  keep.stat = c('rsq','adj.rsq', 'n'), dep.var.labels = depvar,
                  column.labels = c('0--30\\%', '30--70\\%', '70--100\\%'),
                  title = title, label = str_interp('t:${label}'),
                  add.lines = list(c('Controls', c('Yes', 'Yes', 'Yes'))))
  
  means3 = str_interp('No Cover mean & ${mean} \\\\')
  out3 = star_notes_tex(out, note.type = 'threeparttable', note = tablenotes[["sender-twitter-heterogeneity"]])
  
  
  out = c(out1[1:9],
          out1[12:13],
          '\\multicolumn{4}{l}{\\textbf{Panel A: } Number of followers} \\\\ ',
          '\\midrule',
          out1[14:20],
          means1,
          out1[22:23],
          '\\midrule',
          '\\multicolumn{4}{l}{\\textbf{Panel B: } Percentage of audience opposed} \\\\',
          '\\midrule',
          out3[14:20],
          means3,
          out3[22:23],
          '\\midrule',
          out3[21],
          out3[25:length(out3)]
  )
  
  
  
  
  writeLines(out, con = str_interp('output/tables/sender-twitter-heterogeneity.tex'))
  
  

}


main_result_tweet(data)

